-- 自动保护瓦格斯塔夫
local e_util = require "libs/entutil"
local p_util = require "libs/playerutil"
local t_util = require "libs/tableutil"
local function TIPS(str)
    TIP("风暴任务", "yellow", str)
end

local prefabs = { 
    "wagstaff_tool_1", 
    "wagstaff_tool_2", 
    "wagstaff_tool_3",
    "wagstaff_tool_4",
    "wagstaff_tool_5",
}
local needprefab
local h_util = require "libs/hudutil"
for id, prefab in ipairs(prefabs)do
    AddPrefabPostInit(prefab, function(inst)
        inst:DoTaskInTime(.1, function(inst)
            local name = GLOBAL.STRINGS.NAMES["WAGSTAFF_TOOL_"..id]
            GLOBAL.STRINGS.NAMES["WAGSTAFF_TOOL_"..id.."_LAYMAN"] = name
            h_util:CreateLabel(inst, name, nil, GLOBAL.TALKINGFONT, 26, GLOBAL.PLAYERCOLOURS.THISTLE)
        end)
        inst:DoPeriodicTask(.5, function(inst)
            local l = inst._hx_label and inst._hx_label.text
            if l then
                if prefab == needprefab then
                    l:SetSize(20)
                    l:SetColour(GLOBAL.unpack(GLOBAL.PLAYERCOLOURS.CORAL))
                else
                    l:SetSize(16)
                    l:SetColour(GLOBAL.unpack(GLOBAL.PLAYERCOLOURS.THISTLE))
                end
            end
        end)
    end)
end
AddPrefabPostInit("wagstaff_npc", function(inst)
    inst:DoTaskInTime(0.25, function(inst)
        needprefab = nil
        local _Say = inst.components and inst.components.talker and inst.components.talker.Say
        if not _Say then return TIPS("发生未知错误！") end
        inst.components.talker.Say = function(self, str_say, ...)
            for i, prefab in ipairs(prefabs)do
                if str_say == GLOBAL.STRINGS["WAGSTAFF_NPC_WANT_TOOL_"..i] then
                    needprefab = prefab
                    break
                end
            end
            if needprefab then
                for i=1,2 do
                    if str_say == GLOBAL.STRINGS.WAGSTAFF_NPC_YES_THIS_TOOL[i] then
                        needprefab = nil
                        break
                    end
                end
            end
            return _Say(self, str_say, ...)
        end
    end)
end)

------------------ 我是可爱的分界线 ----------------------
local thread
local id_thread = "HUXI_AUTO_WAGSTADFF"
local function StopThread(message)
    GLOBAL.KillThreadsWithID(id_thread)
    if thread then
        TIPS(message or "结束！")
    end
    thread = nil
end
local dontfight = true
local mode = "等待模式"

-- 保护科学家24+16范围内 鸟的8范围内的鸟即可视为一群
local bird_prefabs = {"bird_mutant", "bird_mutant_spitter"}
local bird_core
local function SetCore(bird)
    bird_core = bird:GetPosition()
end

local function Fn()
    local npc = e_util:FindEnt(nil, "wagstaff_npc"
        -- ,nil, nil, nil, nil, nil, function(npc) return e_util:FindEnt(npc, "moonstorm_static", 4) end
)
    if not npc then
        return TIP("风暴任务", "red", "无法启动, 找不到瓦格斯塔夫", "chat")
    end
    if thread then return end
    dontfight = GLOBAL.TheInput:IsKeyDown(GLOBAL.KEY_CTRL) 
    TIP("风暴任务", "green", dontfight and "启动！但是不要自动打鸟！" or "启动！如果不需要自动打鸟，请在启动该功能时同时按住CTRL", "chat")
    local function TMode(str)
        mode = str
        TIPS(mode)
    end
    thread = GLOBAL.StartThread(function()
        while thread and e_util:IsValid(npc) do
            if mode == "战斗模式" then
                if p_util:GetActiveItem() then 
                    p_util:ReturnActiveItem()
                else
                    local bird = e_util:FindEnt(bird_core, bird_prefabs, 9)
                    if bird then
                        if p_util:CanAttack(bird) then
                            local controller = GLOBAL.ThePlayer.components.playercontroller
                            if controller then
                                controller:DoAttackButton(bird)
                            else
                                TIPS("发生错误！")
                                break
                            end
                        else
                            p_util:Click(bird:GetPosition())
                        end
                    else
                        TMode("等待模式")
                    end
                end
            elseif mode == "等待模式" then
                if e_util:IsValid(npc) then
                    p_util:Click(npc:GetPosition())
                    GLOBAL.Sleep(1)
                else
                    break
                end
                local bird = e_util:FindEnt(npc, bird_prefabs, 12)          -- 虽然40是刷新范围，但是太大了，会陷入无尽攻击
                if bird and not dontfight then
                    SetCore(bird)
                    TMode("战斗模式")
                else
                    if needprefab then
                        if p_util:GetActiveItem(needprefab) or p_util:GetItemFromAll(needprefab) then
                            TMode("递交模式")
                        elseif e_util:FindEnt(npc, needprefab, 30) then   -- 其实最高是26
                            TMode("捡拾模式")
                        end
                    end
                end
            elseif mode == "捡拾模式" then
                local tool = e_util:FindEnt(npc, needprefab, 30)
                if tool and needprefab then
                    GLOBAL.ThePlayer:DoTaskInTime(0, function()
                        if e_util:IsValid(tool) then p_util:TryClick(tool, "PICKUP") end
                    end)
                else
                    TMode("递交模式")
                end
            elseif mode == "递交模式" then
                if needprefab then
                    local act_item = p_util:GetActiveItem()
                    if act_item then
                        if act_item.prefab ~= needprefab then
                            local newit = p_util:GetSlotFromAll(needprefab)
                            if newit then
                                local cont = e_util:GetContainer(newit.container)
                                if cont then
                                    cont:SwapActiveItemWithSlot(newit.slot) 
                                end
                            else
                                TMode("等待模式")
                            end
                        else
                            p_util:TryClick(npc, "GIVE")
                        end
                    else
                        local newit = p_util:GetSlotFromAll(needprefab)
                        if newit then
                            local cont = e_util:GetContainer(newit.container)
                            if cont then cont:TakeActiveItemFromAllOfSlot(newit.slot) end
                        else
                            TMode("等待模式")
                        end
                    end
                else
                    TMode("等待模式")
                end
            end
            GLOBAL.Sleep(GLOBAL.FRAMES*3)
        end
        local item = e_util:FindEnt(nil, "moonstorm_static_item")
        if item and GLOBAL.ThePlayer then 
            GLOBAL.ThePlayer:DoTaskInTime(0, function()
                if e_util:IsValid(item) then p_util:TryClick(item, "PICKUP") end
            end)
        end
        StopThread()
    end, id_thread)
end

-----------------------经典老三样-----------------------------
if GetModConfigData("wagstaff_tool_giver") == "biubiu" then
    DEAR_BTNS:AddDearBtn(GLOBAL.GetInventoryItemAtlas("moonstorm_static_item.tex"), "moonstorm_static_item.tex", "风暴任务", "帮助瓦格斯塔夫！小提示：按住CTRL时点击不打鸟", true, Fn)
end
AddBindBtn("wagstaff_tool_giver", Fn)
InterruptedByMobile(function() return thread end, StopThread)